package com.asiainfo.complier.ui;

import com.asiainfo.complier.helper.UIHelper;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;

public class JPropertiesDialog extends JWindow {

    /**
     * JPropertiesDialog
     */
    private static final long serialVersionUID = 425351866925297789L;

    private JPanel leftPanel;
    private JPanel rightPanel;
    GridBagConstraints c;
    private JLabel lbTitle;
    private ArrayList<JProperitesItem> itemList;

    /**
     * Initial dialog
     */
    public JPropertiesDialog(JFrame parent, String title) {
        super(parent);
        itemList = new ArrayList<JProperitesItem>();
        leftPanel = new JPanel();
        final JPanel topPanel = new JPanel();
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(new ColorBorder(new Color(0xB2, 0xB9, 0xC1)));

        final JLabel exitLabel;

        topPanel.setLayout(new BorderLayout(2, 2));
        topPanel.add(
                exitLabel = new JLabel(UIHelper
                        .getImageIco("com/asiainfo/images/exit.png")),
                BorderLayout.EAST);
        lbTitle = new JLabel();
        topPanel.add(lbTitle, BorderLayout.WEST);
        lbTitle.setBorder(new EmptyBorder(0, 10, 0, 0));
        exitLabel.setBorder(new EmptyBorder(0, 10, 0, 5));
        exitLabel.setToolTipText("�ر�");
        exitLabel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                dispose();
            }
        });
        topPanel.setBackground(new Color(0xDA, 0xDC, 0xDD));
        topPanel.addMouseMotionListener(new MouseMotionAdapter() {

            public void mouseDragged(MouseEvent e) {

                int dx = e.getX();
                int dy = e.getY();
                int cx = getLocation().x + dx - getWidth() / 2;
                int cy = getLocation().y + dy;
                Point point = new Point(cx, cy);
                setLocation(point);

            }
        });

        exitLabel.addMouseListener(new MouseAdapter() {

            public void mouseExited(MouseEvent e) {
                exitLabel.setIcon(UIHelper
                        .getImageIco("com/asiainfo/images/exit.png"));
            }

            public void mouseEntered(MouseEvent e) {
                exitLabel.setIcon(UIHelper
                        .getImageIco("com/asiainfo/images/exit2.png"));
            }

            public void mouseClicked(MouseEvent e) {
                dispose();
            }

        });
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());
        JPanel btPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        // this.setTitle(title);
        c = new GridBagConstraints();
        // leftPanel.setLayout(new GridBagLayout());
        leftPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));

        leftPanel.setPreferredSize(new Dimension(100, -1));
        leftPanel.setBackground(new Color(0xE4, 0xE4, 0xE4));
        leftPanel.setBorder(new Border() {

            public void paintBorder(Component c, Graphics g, int x, int y,
                                    int width, int height) {
                g.setColor(new Color(0x9E, 0x9E, 0x9E));

                g.drawLine(c.getWidth() - 1, 0, c.getWidth() - 1,
                        c.getHeight() - 1);

            }

            public boolean isBorderOpaque() {

                return true;
            }

            public Insets getBorderInsets(Component c) {

                return new Insets(1, 0, 1, 1);
            }
        });

        topPanel.setPreferredSize(new Dimension(-1, 26));
        topPanel.setBorder(new Border() {

            public void paintBorder(Component c, Graphics g, int x, int y,
                                    int width, int height) {

                g.setColor(new Color(0x9E, 0x9E, 0x9E));
                g.drawLine(0, height - 1, width, height - 1);

            }

            public boolean isBorderOpaque() {

                return true;
            }

            public Insets getBorderInsets(Component c) {

                return new Insets(2, 2, 2, 2);
            }
        });

        btPanel.setBackground(new Color(0xDA, 0xDC, 0xDD));
        btPanel.setBorder(new Border() {

            public void paintBorder(Component c, Graphics g, int x, int y,
                                    int width, int height) {
                g.setColor(new Color(0xB2, 0xB9, 0xC1));
                g.drawLine(0, 0, getWidth(), 0);
            }

            public boolean isBorderOpaque() {

                return true;
            }

            public Insets getBorderInsets(Component c) {

                return new Insets(1, 1, 1, 1);
            }
        });
        btPanel.add(new JRoundButton("���沢�ر�"));
        btPanel.add(new JRoundButton("����"));
        btPanel.add(new JRoundButton("ȡ��"));

        rightPanel = new JPanel();

        contentPanel.setLayout(new BorderLayout());
        contentPanel.add(leftPanel, BorderLayout.WEST);
        contentPanel.add(rightPanel, BorderLayout.CENTER);

        rightPanel.setLayout(new BorderLayout());

        mainPanel.add(topPanel, BorderLayout.NORTH);
        mainPanel.add(contentPanel, BorderLayout.CENTER);
        mainPanel.add(btPanel, BorderLayout.SOUTH);

        this.getContentPane().add(mainPanel);

    }

    public void setTitle(String title) {

        lbTitle.setText(title);
    }

    public void addProperitiesPanel(String title, JProperitesItem item) {

        final JTitleItem lb = new JTitleItem(title, leftPanel);
        lb.setItem(item);
        lb.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {

                rightPanel.removeAll();
                rightPanel.add(lb.getItem(), BorderLayout.CENTER);

            }
        });
        leftPanel.add(lb);
        itemList.add(item);

    }
}
